﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DMS.Business.Modules.ArchivesManagement;
using DMS.Business.Utility;
using DMS.Data;

namespace DMS.Web.Modules.ArchivesManagement
{
    public partial class AttachmentList : System.Web.UI.UserControl
    {
        public string ArchivesID { get; set; }

        private IQueryable<AM_Attachment> Attachments { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControls();
                Search();
            }
        }

        private void InitControls()
        {
            if (!SystemContext.Current.LoginUser.IsInRole("档案管理员;系统管理员;"))
            {
                this.MainHolder_btnSearch.Visible = false;

                attachmentList.Columns[6].Visible = false;
            }
        }

        protected string GetCondition()
        {
            string condition = "";

            condition += string.Format(" and it.ArchivesID='{0}'", ArchivesID);

            //if(ddlBelongYear.SelectedValue!="")
            //{
            //    condition += string.Format(" and it.BelongYear = '{0}'", ddlBelongYear.SelectedValue);
            //}
            //if(ddlBelongMonth.SelectedValue!="")
            //{
            //    condition += string.Format(" and it.BelongMonth = '{0}'", ddlBelongMonth.SelectedValue);
            //}
            //if (txtName.Text != "")
            //{
            //    condition += string.Format(" and it.Name like '%{0}%'", txtName.Text.Trim());
            //}
            //if (txtType.SelectedValue != "")
            //{
            //    condition += string.Format(" and it.Type = '{0}'", txtType.SelectedValue);
            //}
            //if (txtInitDateBegin.Text != "")
            //{
            //    condition += string.Format(" and it.InitDate >= cast('{0}' as System.DateTime)", DateTime.Parse(txtInitDateBegin.Text));
            //}
            //if (txtInitDateEnd.Text != "")
            //{
            //    condition += string.Format(" and it.InitDate <= cast('{0}' as System.DateTime)", DateTime.Parse(txtInitDateEnd.Text));
            //}
            //if (txtBeginTimeBegin.Text != "")
            //{
            //    condition += string.Format(" and it.CustodyBeginDate >= cast('{0}' as System.DateTime)", DateTime.Parse(txtBeginTimeBegin.Text));
            //}
            //if (txtBeginTimeEnd.Text != "")
            //{
            //    condition += string.Format(" and it.CustodyBeginDate <= cast('{0}' as System.DateTime)", DateTime.Parse(txtBeginTimeEnd.Text));
            //}
            //if (txtExpireTimeBegin.Text != "")
            //{
            //    condition += string.Format(" and it.CustodyEndDate >= cast('{0}' as System.DateTime)", DateTime.Parse(txtExpireTimeBegin.Text));
            //}
            //if (txtExpireTimeEnd.Text != "")
            //{
            //    condition += string.Format(" and it.CustodyEndDate <= cast('{0}' as System.DateTime)", DateTime.Parse(txtExpireTimeEnd.Text));
            //}
            //if (txtPersonName.Text != "")
            //{
            //    condition += string.Format(" and it.PersonName like '%{0}%'", txtPersonName.Text.Trim());
            //}
            //if (ddlDestroy.SelectedValue != "")
            //{
            //    condition += string.Format(" and it.IsDestroy = {0}", ddlDestroy.SelectedValue);
            //}

            return condition;
        }

        private void Search()
        {
            int total;
            string condition = GetCondition();

            if (this.PageSize.Text == "")
                this.PageSize.Text = "15";
            pager.PageSize = int.Parse(this.PageSize.Text);
            List<AM_Attachment> list = AttachmentService.GetAttachments(SystemContext.Current.LoginAuthUser, condition,
                                                                        pager.CurrentPageIndex, pager.PageSize,
                                                                        out total);

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.attachmentList.Attributes["SortExpression"];
            string sortDirection = this.attachmentList.Attributes["SortDirection"];

            IQueryable<AM_Attachment> queryableList;
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                //调用排序方法对当前页数据进行排序
                queryableList = (list.AsQueryable()).OrderUsingSortExpression(sortExpression + " " + sortDirection);
            }
            else
            {
                queryableList = list.AsQueryable();
            }

            Attachments = queryableList;

            // GridView绑定并显示数据
            this.attachmentList.DataSource = queryableList;
            this.attachmentList.DataBind();

            pager.RecordCount = total;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            Search();
        }

        protected void DealCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            var id = e.CommandArgument as string;
            if (commandName.Equals("DeleteAttachment"))
            {
                AttachmentService.DeleteAttachInfo(id);
            }
            Search();
        }

        //列表的排序事件
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == attachmentList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (attachmentList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            attachmentList.Attributes["SortExpression"] = sortExpression;
            attachmentList.Attributes["SortDirection"] = sortDirection;

            Search();
        }

        protected void List_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //AM_Attachment baseInfo = Attachments.ToList()[e.Row.RowIndex];

                //转化保险类型
                AM_Attachment  attachment  = (AM_Attachment )e.Row.DataItem;
                string[] imgType = {"jpg", "gif", "png", "jpeg", "bmp"};
                HtmlImage imgUrlFiled = e.Row.FindControl("imgUrlFiled") as HtmlImage;
                HtmlAnchor printAnchor = e.Row.FindControl("printBtn") as HtmlAnchor;
                if (!imgType.Contains(attachment.AttachURL.Split('.')[1]))
                {
                    imgUrlFiled.Src = "/Resources/Images/noPhotos.jpg";
                    imgUrlFiled.Attributes["onclick"] = "";
                    printAnchor.Visible = false;
                }else
                {
                    imgUrlFiled.Src = "../../Attachments/"+attachment.AttachURL;
                    printAnchor.HRef = "javascript:printImg(\"../../Attachments/" + attachment.AttachURL + "\")";
                }
            }
        }
    }
}